﻿using System;
using System.Collections.Concurrent;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;


namespace FinalTest_Algonrithm
{

    public class FinalTest_AlgorithmMgr
    {
        public ParamIn param = new ParamIn();
        public double PixelX, PixelY, Tz;
        public double COD_X, COD_Y;
        public bool bLight;
        public string _Path;
        private FinalTest_AlgorithmMgr()
        {
        }
        private static object obj = new object();
        private static FinalTest_AlgorithmMgr moduleMgr;
        private ConcurrentDictionary<int, FinalTest_AlgorithmBase> m_lisDevice = new ConcurrentDictionary<int, FinalTest_AlgorithmBase>();
        private bool bLoad = false;
        public static FinalTest_AlgorithmMgr Instance
        {
            get
            {
                lock (obj)
                {
                    if (moduleMgr == null)
                    {

                        if (moduleMgr == null)
                        {
                            moduleMgr = new FinalTest_AlgorithmMgr();
                        }

                    }
                    return moduleMgr;
                }
            }
        }

        public void AddAlgorithm(int nID, string strName)
        {
            Assembly assembly = Assembly.GetAssembly(typeof(FinalTest_AlgorithmBase));
            string name = "FinalTest_Algonrithm.Alg_" + strName;
            //Motion_Advantech
            Type type = assembly.GetType(name);
            bool flag = type == null;
            if (flag)
            {
                throw new Exception($"算法类型{strName}找不到可用的封装类，请确认该类型DLL或配置错误");
            }
            object[] args = new object[] { };
            if (!m_lisDevice.ContainsKey(nID))
            {
                m_lisDevice.TryAdd(nID, Activator.CreateInstance(type, args) as FinalTest_AlgorithmBase);
            }
            else
            {
                m_lisDevice[nID] = Activator.CreateInstance(type, args) as FinalTest_AlgorithmBase;
            }
        }
        public FinalTest_AlgorithmBase GetAlgByIndexID(int index = 0)
        {
            FinalTest_AlgorithmBase temp = m_lisDevice[index];
            return temp;
        }
        public bool LoadConfig(string path)
        {
            _Path = Path.GetDirectoryName(path) + "\\";
            FinalTest_AlgorithmBase tempModuleBase = GetAlgByIndexID();
            if (tempModuleBase == null)
            {
                return false;
            }
            if (!tempModuleBase.LoadConfig(path))
            {
                return false;
            }
            bLoad = true;
            return true;
        }



        public int GetLight(Bitmap bitmap, float[] Lightness, int iStation = 0)
        {
            if (!bLoad)
            {
                return -1;
            }
            try
            {
                FinalTest_AlgorithmBase tempModuleBase = GetAlgByIndexID();
                if (tempModuleBase != null)
                {
                    return tempModuleBase.GetLight(bitmap, Lightness, iStation);
                }
                else
                {
                    return -2;
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return -99;
            }

        }
        public int GetOC(Bitmap bitmap, ref float OCx, ref float OCy, int iStation = 0)
        {
            if (!bLoad)
            {
                return -1;
            }
            try
            {
                FinalTest_AlgorithmBase tempModuleBase = GetAlgByIndexID();
                if (tempModuleBase != null)
                {
                    return tempModuleBase.GetOC(bitmap, ref OCx, ref OCy, iStation);
                }
                else
                {
                    return -2;
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return -99;
            }
        }

        public int GetFOV(Bitmap bitmap, ref float HFOV, ref float VFOV, ref float DFOV, ref Bitmap OutBitmap, int iStation = 0)
        {
            if (!bLoad)
            {
                return -1;
            }
            try
            {
                FinalTest_AlgorithmBase tempModuleBase = GetAlgByIndexID();
                if (tempModuleBase != null)
                {
                    return tempModuleBase.GetFOV(bitmap, ref HFOV, ref VFOV, ref DFOV, ref OutBitmap, iStation);
                }
                else
                {
                    return -2;
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return -99;
            }

        }

        public int Getmirror(Bitmap bitmap, ref bool IsMirrored, ref Bitmap OutBitmap, int iStation = 0)
        {
            if (!bLoad)
            {
                return -1;
            }
            try
            {
                FinalTest_AlgorithmBase tempModuleBase = GetAlgByIndexID();
                if (tempModuleBase != null)
                {
                    return tempModuleBase.Getmirror(bitmap, ref IsMirrored, ref OutBitmap, iStation);
                }
                else
                {
                    return -2;
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return -99;
            }

        }


        public bool GetSFRValue(Bitmap p, ref SFRValue SFRValue, ref RectInfo rectInfo, ref LightValue light, int iStation = 0)
        {
            if (!bLoad)
            {
                return false;
            }
            try
            {
                FinalTest_AlgorithmBase tempModuleBase = GetAlgByIndexID();
                if (tempModuleBase != null)
                {
                    return tempModuleBase.GetSFRValue(p, ref SFRValue, ref rectInfo, ref light);
                }
                else
                {
                    return false;
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return false;
            }

        }

        public int GetAWB(Bitmap bitmap, float[] R, float[] G, float[] B, float[] RvsG, float[] BvsG, ref Bitmap OutBitmap, int iStation = 0)
        {
            if (!bLoad)
            {
                return -1;
            }
            try
            {
                FinalTest_AlgorithmBase tempModuleBase = GetAlgByIndexID();
                if (tempModuleBase != null)
                {
                    return tempModuleBase.GetAWB(bitmap, R, G, B, RvsG, BvsG, ref OutBitmap, iStation);
                }
                else
                {
                    return -2;
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return -99;
            }

        }


        public int GetBlemish(Bitmap bitmap, ref int StainNum, int[] StainArea, ref Bitmap OutBitmap, int iStation = 0)
        {
            if (!bLoad)
            {
                return -1;
            }
            try
            {
                FinalTest_AlgorithmBase tempModuleBase = GetAlgByIndexID();
                if (tempModuleBase != null)
                {
                    return tempModuleBase.GetBlemish(bitmap, ref StainNum, StainArea, ref OutBitmap, iStation);
                }
                else
                {
                    return -2;
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return -99;
            }

        }

        public int GetParity(Bitmap bitmap, ref int SmallCount, ref int MediumCount, ref int BigCount, ref Bitmap OutBitmap, int iStation = 0)
        {
            if (!bLoad)
            {
                return -1;
            }
            try
            {
                FinalTest_AlgorithmBase tempModuleBase = GetAlgByIndexID();
                if (tempModuleBase != null)
                {
                    return tempModuleBase.GetParity(bitmap, ref SmallCount, ref MediumCount, ref BigCount, ref OutBitmap, iStation);
                }
                else
                {
                    return -2;
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return -99;
            }

        }

        public int GetShading(Bitmap bitmap, ref float RatioUL, ref float RatioUR, ref float RatioDL, ref float RatioDR, ref float Ratio_Diff, ref Bitmap OutBitmap, int iStation = 0)
        {
            if (!bLoad)
            {
                return -1;
            }
            try
            {
                FinalTest_AlgorithmBase tempModuleBase = GetAlgByIndexID();
                if (tempModuleBase != null)
                {
                    return tempModuleBase.GetShading(bitmap, ref RatioUL, ref RatioUR, ref RatioDL, ref RatioDR, ref Ratio_Diff, ref OutBitmap, iStation);
                }
                else
                {
                    return -2;
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return -99;
            }

        }

        public int GetColor6(Bitmap bitmap, float[] ColorInfo, ref Bitmap OutBitmap, int iStation = 0)
        {
            if (!bLoad)
            {
                return -1;
            }
            try
            {
                FinalTest_AlgorithmBase tempModuleBase = GetAlgByIndexID();
                if (tempModuleBase != null)
                {
                    return tempModuleBase.GetColor6(bitmap, ColorInfo, ref OutBitmap, iStation);
                }
                else
                {
                    return -2;
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return -99;
            }

        }

        public int GetColor24(Bitmap bitmap, float[] ColorInfo, ref Bitmap OutBitmap, int iStation = 0)
        {
            if (!bLoad)
            {
                return -1;
            }
            try
            {
                FinalTest_AlgorithmBase tempModuleBase = GetAlgByIndexID();
                if (tempModuleBase != null)
                {
                    return tempModuleBase.GetColor24(bitmap, ColorInfo, ref OutBitmap, iStation);
                }
                else
                {
                    return -2;
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return -99;
            }





        }
        public int GeGray(Bitmap bitmap, float[] GrayInfo, float[] GrayDiffInfo, int[] DiffCount, ref Bitmap OutBitmap, int iStation = 0)
        {
            if (!bLoad)
            {
                return -1;
            }
            try
            {
                FinalTest_AlgorithmBase tempModuleBase = GetAlgByIndexID();
                if (tempModuleBase != null)
                {
                    return tempModuleBase.GeGray(bitmap, GrayInfo, GrayDiffInfo, DiffCount, ref OutBitmap, iStation);
                }
                else
                {
                    return -2;
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return -99;
            }

        }



        public int GeHotpixel(Bitmap bitmap, ref int SingleCount, ref int DoubleCount, ref int MultipleCount, ref Bitmap OutBitmap, int iStation = 0)
        {
            if (!bLoad)
            {
                return -1;
            }
            try
            {
                FinalTest_AlgorithmBase tempModuleBase = GetAlgByIndexID();
                if (tempModuleBase != null)
                {
                    return tempModuleBase.GeHotpixel(bitmap,ref SingleCount,ref DoubleCount,ref MultipleCount, ref OutBitmap, iStation);
                }
                else
                {
                    return -2;
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return -99;
            }

        }

        public int GeFlare(Bitmap bitmap, ref float ResultValue, ref Bitmap OutBitmap, int iStation = 0)
        {
            if (!bLoad)
            {
                return -1;
            }
            try
            {
                FinalTest_AlgorithmBase tempModuleBase = GetAlgByIndexID();
                if (tempModuleBase != null)
                {
                    return tempModuleBase.GeFlare(bitmap,ref ResultValue, ref OutBitmap, iStation);
                }
                else
                {
                    return -2;
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return -99;
            }

        }

        public int GeSNR(Bitmap bitmap, ref float ResultValue, ref Bitmap OutBitmap, int iStation = 0)
        {
            if (!bLoad)
            {
                return -1;
            }
            try
            {
                FinalTest_AlgorithmBase tempModuleBase = GetAlgByIndexID();
                if (tempModuleBase != null)
                {
                    return tempModuleBase.GeSNR(bitmap,ref ResultValue, ref OutBitmap, iStation);
                }
                else
                {
                    return -2;
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return -99;
            }

        }

        public int GeIRLED(Bitmap bitmap, ref float MeanValue, ref Bitmap OutBitmap, int iStation = 0)
        {
            if (!bLoad)
            {
                return -1;
            }
            try
            {
                FinalTest_AlgorithmBase tempModuleBase = GetAlgByIndexID();
                if (tempModuleBase != null)
                {
                    return tempModuleBase.GeIRLED(bitmap, ref MeanValue, ref OutBitmap, iStation);
                }
                else
                {
                    return -2;
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return -99;
            }

        }
    }
}
